package com.batyuta;

/**
 * Created by IntelliJ IDEA.
 * User: alexey
 * Date: 6/20/11
 * Time: 4:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class Triangle implements ITriangle {
    private Integer len1 = -1;
    private Integer len2 = -1;
    private Integer len3 = -1;

    public Triangle(Integer len1, Integer len2, Integer len3) {
        this.len1 = len1;
        this.len2 = len2;
        this.len3 = len3;
    }

    public Integer getLen1() {
        return len1;
    }

    public Integer getLen2() {
        return len2;
    }

    public Integer getLen3() {
        return len3;
    }

    public TriangleResult isValid() {
        TriangleResult result = TriangleResult.INVALID_DATA;
        if (getLen1() == null || getLen2() == null || getLen3() == null) {
            return result;
        }
        if (getLen1() <= 0 || getLen2() <= 0 || getLen3() <= 0) {
            return result;
        }
        if (isTriangle()) {
            result = TriangleResult.TRIANGLE;
            if (isIsosceles()) {
                //равнобедренный
                result = TriangleResult.TRIANGLE_ISOSCELES;
                if (isEquilateral()) {
                    // равносторонний
                    result = TriangleResult.TRIANGLE_EQUILATERAL;
                }
            }
        }

        return result;
    }

    /**
     * равнобедренный
     *
     * @return
     */
    private boolean isIsosceles() {
        return getLen1().equals(getLen2()) || getLen2().equals(getLen3()) || getLen3().equals(getLen1());
    }

    /**
     * равносторонний
     *
     * @return
     */
    private boolean isEquilateral() {
        return getLen1().equals(getLen2()) && getLen2().equals(getLen3());
    }

    private boolean isTriangle() {
//        double p = ((double) (getLen1() + getLen2() + getLen3())) / 2d;
//        double v = (p - getLen1()) * (p - getLen2()) * (p - getLen3());
//        return v > 0;
        return (getLen1()+getLen2()>getLen3()) &&(getLen2()+getLen3()>getLen1()) &&(getLen3()+getLen1()>getLen2());
    }

    @Override
    public String toString() {
        return "Triangle{" +
                "len1=" + len1 +
                ", len2=" + len2 +
                ", len3=" + len3 +
                '}';
    }
}
